package com.gxuwz.zlc.controller;

import com.gxuwz.zlc.entity.Assistant;
import com.gxuwz.zlc.entity.LabTechnician;
import com.gxuwz.zlc.entity.Student;
import com.gxuwz.zlc.entity.User;
import com.gxuwz.zlc.service.AssistantService;
import com.gxuwz.zlc.service.LabTechnicianService;
import com.gxuwz.zlc.service.StudentService;
import com.gxuwz.zlc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class GetUserSessionController {

    // 用户
    @Autowired
    private UserService userService;
    // 学生
    @Autowired
    private StudentService studentService;
    // 技术人员
    @Autowired
    private LabTechnicianService labTechnicianService;
    // 助理表
    @Autowired
    private AssistantService assistantService;

    /**
     * 获取用户的session
     * @param request
     * @param httpSession
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getUserSession", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Map getUserSession(HttpServletRequest request, HttpSession httpSession) {
        Map<String,String> map = new HashMap<>();
        String username = (String) httpSession.getAttribute("user");
        // 根据username(用户id)从user表中查询角色，判断后再去查询信息
        User user = userService.queryUserByName(username);
        if ("student".equals(user.getPerms())){
            // 从学生表中查询
            Student studentById = studentService.findStudentById(username);
            map.put("id",studentById.getStuId());
            map.put("name",studentById.getStuName());
            map.put("perms","学生");
        }else if ("technician".equals(user.getPerms())){
            // 从技术人员表中查询
            LabTechnician labTechnicianById = labTechnicianService.findLabTechnicianById(username);
            map.put("id",labTechnicianById.getTechId());
            map.put("name",labTechnicianById.getTechName());
            map.put("perms","实验室技术人员");
        }else if ("assistant".equals(user.getPerms())){
            // 助理（也从学生表中查询）
//            Student studentById = studentService.findStudentById(username);
//            map.put("id",studentById.getStuId()); // 学号
//            map.put("name",studentById.getStuName()); // 姓名
            // 从助理表查询
            Assistant assistantByStuId = assistantService.findAssistantByStuId(username);
            map.put("id",assistantByStuId.getStuId()); // 学号
            map.put("name",assistantByStuId.getStuName()); // 姓名
            map.put("assistantId1",assistantByStuId.getAssistantId()); // 助理编号
            map.put("perms","实验室助理");
        }else if ("studentOffice".equals(user.getPerms())){
            // 学工处
            map.put("name",user.getUsername());
            map.put("perms","学工处");
        }else if ("admin".equals(user.getPerms())){
            // 管理员
            map.put("name",user.getUsername());
            map.put("perms","系统管理员");
        }
        map.put("code","200");
        return map;
    }


}
